package com.iss.card.view.store;

import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.Gallery.LayoutParams;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.iss.card.R;
import com.iss.card.cache.BitmapConfig;
import com.iss.card.cache.ImageDataManager;
import com.iss.card.common.ApplicationManager;
import com.iss.card.common.CommonValue;
import com.iss.card.model.ProductDetailInfo;
import com.iss.card.view.runable.DownPicHandler;

/**
 * ������ϸGallery��ʾͼƬ
 */
public class ProductListAdapter extends BaseAdapter implements
		com.iss.card.view.runable.DownPicHandler.ImageCallback {
	private static final String TAG = ProductListAdapter.class.getSimpleName();
	private Context mContext;
	private List<ProductDetailInfo> mContents = new ArrayList<ProductDetailInfo>();
	private DownPicHandler downPicHandler;
	private Gallery mGallery;

	public ProductListAdapter() {
		super();
	}

	public ProductListAdapter(Activity activity, Gallery gallery) {
		mContext = activity;
		downPicHandler = new DownPicHandler(this);
		mGallery = gallery;
	}

	public void setContents(List<ProductDetailInfo> list) {
		mContents.clear();
		mContents.addAll(list);
	}

	@Override
	public int getCount() {
		return mContents.size();
	}

	@Override
	public Object getItem(int position) {
		return mContents.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		MyImgViewHolder hv = null;
		final int w = ApplicationManager.SCREEN_WIDTH;
		ProductDetailInfo info = mContents.get(position);
		String url = info.getProductPicUrl();
		if (convertView == null) {
			hv = new MyImgViewHolder();
			/*convertView = LayoutInflater.from(mContext).inflate(
					R.layout.store_detail_product_item, null);*/
			
			MyImageView imgView = new MyImageView(mContext, w, (info.getProductPicH() * w / info
					.getProductPicW()));
			convertView = imgView;
			hv.iv = imgView;
			/*hv.iv = (ImageView) convertView
					.findViewById(R.id.iv_store_detail_product_item);*/
			hv.iv.setLayoutParams(new Gallery.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
			convertView.setTag(hv);
		} else {
			hv = (MyImgViewHolder) convertView.getTag();
		}
		
		Log.i(TAG, position + "---" + url);

		hv.iv.setTag(url);
		
		ImageDataManager.instance().loadBitmap(
				url,
				hv.iv,
				new BitmapConfig(w, (info.getProductPicH() * w / info
						.getProductPicW())));
		return convertView;
	}

	class HoldView {
		ImageView iv;
	}
	static class MyImgViewHolder{
		MyImageView iv;
	}

	@Override
	public void imageLoaded(Bitmap imageBitmap, String imageUrl) {
		// TODO Auto-generated method stub
		ImageView imageViewByTag = (ImageView) mGallery
				.findViewWithTag(imageUrl);
		if (imageViewByTag != null) {
			Log.i(TAG, "imageLoaded:" + imageUrl);
			imageViewByTag.setImageBitmap(imageBitmap);
		}
	}

	public void removeThread() {
		// int size = mUrl.length;
		// for(int i = 0;i < size;i++){
		// SoftReference<Bitmap> bitmap = CommonValue.mHashMapBitmap.get(mUrl);
		// if(bitmap !=null && bitmap.get() != null){
		// bitmap.get().recycle();
		// }
		// }
		downPicHandler.remove();
	}

	private List<ProductDetailInfo> copyProductList(List<ProductDetailInfo> list) {
		List<ProductDetailInfo> products = new ArrayList<ProductDetailInfo>();
		for (ProductDetailInfo info : list) {
			products.add(info);
		}
		return products;
	}
}
